package com.dick.base.api;

import com.dick.base.dto.*;
import com.dick.base.model.BusinessCustomer;
import com.dick.base.model.BusinessRoll;
import com.dick.base.model.ProduceItem;
import com.dick.base.model.ProduceShelf;
import com.dick.base.parameter.*;
import com.dick.base.service.ProduceService;
import com.dick.base.session.util.SessionUtil;
import com.dick.base.util.BaseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * 生产过程api
 */
@RestController
@RequestMapping("api/produce")
public class ProduceApi {

    private static final String TAG = "生产";

    @Autowired
    private ProduceService produceService;

    @ApiOperation(value = "待产列表", tags = TAG)
    @GetMapping("to-be-produced")
    public BaseResult<ToBeProduceList> toBeProduceList(String material, String thickness,
                                                       Integer width, Long customerId) {
        return BaseResult.of(produceService.fillInMemoAndUrgent(produceService.produceList(material, thickness, width,
                ProduceItem.STATUS_TO_BE_PRODUCED, customerId)));
    }

    @ApiOperation(value = "添加生产", tags = TAG)
    @PostMapping("add")
    public BaseResult<Void> addProduce(@RequestBody @Valid ProduceIdList addProduceIdList) {
        produceService.addProduceId(addProduceIdList, SessionUtil.currentUser().getId());
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "生产列表", tags = TAG)
    @GetMapping("produce-list")
    public BaseResult<ToBeProduceList> produceList() {
        return BaseResult.of(produceService.fillInMemoAndUrgent(produceService.produceList(SessionUtil.currentUser().getId())));
    }

    @ApiOperation(value = "生产完成", tags = TAG)
    @PostMapping("produce-complete")
    public BaseResult<Void> produceComplete(@RequestBody @Valid ProduceIdList produceIdList) {
        produceService.produceComplete(produceIdList, SessionUtil.currentUser().getId());
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "可用卷查询", tags = TAG)
    @GetMapping(path = "rolls-available")
    public BaseResult<List<BusinessRoll>> rollPage(@RequestParam String material,
                                                   @RequestParam String thickness,
                                                   @RequestParam Integer width) {
        return BaseResult.of(produceService.rollPage(material, thickness, width, SessionUtil.currentUser().getId()));
    }

    @ApiOperation(value = "上料", tags = TAG)
    @PostMapping("rolls/put-up")
    public BaseResult<Void> rollPutUp(@RequestParam Long id) {
        produceService.rollPutUp(id, SessionUtil.currentUser().getId());
        return BaseResult.voidResult();
    }

//    @ApiOperation(value = "剪板", tags = TAG)
//    @PostMapping("rolls/cut")
//    public BaseResult<Void> rollCut(@RequestParam Long id, @RequestParam BigDecimal weight) {
//        produceService.rollCut(id, weight, SessionUtil.currentUser().getId());
//        return BaseResult.voidResult();
//    }

    @ApiOperation(value = "退料", tags = TAG)
    @PostMapping("rolls/withdraw")
    public BaseResult<Void> rollWithdraw(@RequestParam Long id, @RequestParam BigDecimal weight) {
        produceService.rollWithdraw(id, weight, SessionUtil.currentUser().getId());
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "返回待产", tags = TAG)
    @PostMapping("turnover")
    public BaseResult<Void> turnover(@RequestBody @Valid TurnoverParameter parameter) {
        produceService.turnover(parameter, SessionUtil.currentUser().getId());
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "待分板列表", tags = TAG)
    @GetMapping("to-be-packaged")
    public BaseResult<ToBePackageList> toBePackageList(String material, String thickness, Integer width, Long customerId) {
        return BaseResult.of(produceService.shelfList(material, thickness, width, ProduceItem.STATUS_PRODUCED, customerId));
    }

    @ApiOperation(value = "工程待添加托盘生产项列表", tags = TAG)
    @PostMapping("to-be-add-items")
    public BaseResult<List<ProduceItem>> leftItems(@RequestParam String projectVirtualId) {
        return BaseResult.of(produceService.leftItems(projectVirtualId));
    }

    @ApiOperation(value = "添加托盘", tags = TAG)
    @PostMapping("shelf/add")
    public BaseResult<ProduceShelf> shelfAdd(@RequestParam String projectVirtualId) {
        return BaseResult.of(produceService.shelfAdd(projectVirtualId));
    }

    @ApiOperation(value = "编辑托盘", tags = TAG)
    @PostMapping("shelf/modify")
    public BaseResult<ProduceShelf> shelfModify(@RequestBody @Valid ShelfModifyParameter parameter) {
        return BaseResult.of(produceService.shelfModify(parameter));
    }

    @ApiOperation(value = "托盘拥有的生产项列表", tags = TAG)
    @GetMapping("shelf/items")
    public BaseResult<List<ProduceItem>> findProduceItemByShelfId(@RequestParam Long id) {
        return BaseResult.of(produceService.findProduceItemByShelfId(id));
    }

    @ApiOperation(value = "可添加托盘的生产项列表", tags = TAG)
    @GetMapping("shelf/available-items")
    public BaseResult<List<ProduceItem>> findProduceItemAvailableForShelf(@RequestParam Long id) {
        return BaseResult.of(produceService.findProduceItemAvailableForShelf(id));
    }

    @ApiOperation(value = "给托盘添加生产项列表", tags = TAG)
    @PostMapping("shelf/add-items")
    public BaseResult<Void> addItemsToShelf(@RequestBody @Valid ItemsAddToShelfParameter parameter) {
        produceService.addItemsToShelf(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "给托盘删减生产项列表", tags = TAG)
    @PostMapping("shelf/remove-items")
    public BaseResult<Void> removeItemsToShelf(@RequestBody @Valid ItemsAddToShelfParameter parameter) {
        produceService.removeItemsToShelf(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "托盘完成打包", tags = TAG)
    @PostMapping("shelf/package")
    public BaseResult<Void> packageComplete(@RequestParam Long id) {
        produceService.packageComplete(id);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "待合包列表", tags = TAG)
    @GetMapping("to-be-reduced")
    public BaseResult<ToBePackageList> toBeReducedList(String material, String thickness, Integer width, Long customerId) {
        return BaseResult.of(produceService.shelfList(material, thickness, width, ProduceItem.STATUS_PRODUCED, customerId));
    }

    @ApiOperation(value = "执行合包", tags = TAG)
    @PostMapping("shelf/reduce")
    public BaseResult<Void> reduce(@RequestBody @Valid ReduceParameter parameter) {
        produceService.reduce(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "执行改板", tags = TAG)
    @PostMapping("change-specification")
    public BaseResult<Void> change(@RequestBody @Valid ChangeSpecificationParameter parameter) {
        produceService.change(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "执行配板", tags = TAG)
    @PostMapping("match")
    public BaseResult<Void> match(@RequestBody @Valid MatchParameter parameter) {
        produceService.match(parameter);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "可执行合包的客户列表", tags = TAG)
    @GetMapping("customers/reduce")
    public BaseResult<List<BusinessCustomer>> customerListForReduce() {
        return BaseResult.of(produceService.customerList(ProduceItem.STATUS_PRODUCED));
    }

    @ApiOperation(value = "可执行打包和出标签的客户列表", tags = TAG)
    @GetMapping("customers/package")
    public BaseResult<List<BusinessCustomer>> customerListForPackage() {
        return BaseResult.of(produceService.customerList(ProduceItem.STATUS_PRODUCED, ProduceItem.STATUS_PACKAGED));
    }

    @ApiOperation(value = "可执行分板的客户列表", tags = TAG)
    @GetMapping("customers/add")
    public BaseResult<List<BusinessCustomer>> customerListForAdd() {
        return BaseResult.of(produceService.customerList(ProduceItem.STATUS_PRODUCED));
    }

    @ApiOperation(value = "可执行改板的客户列表", tags = TAG)
    @GetMapping("customers/change")
    public BaseResult<List<BusinessCustomer>> customerListForChange() {
        return BaseResult.of(produceService.customerList(ProduceItem.STATUS_TO_BE_PRODUCED));
    }

    @ApiOperation(value = "可打包的托盘列表", tags = TAG)
    @GetMapping("to-be-package-shelf-list")
    public BaseResult<ToBePackageShelfList> toBePackageShelfList(@RequestParam Long customerId) {
        return BaseResult.of(produceService.toBePackageShelfList(customerId, ProduceItem.STATUS_PRODUCED));
    }

    @ApiOperation(value = "已打包的托盘列表", tags = TAG)
    @GetMapping("packaged-shelf-list")
    public BaseResult<ToBePackageShelfList> packagedShelfList(@RequestParam Long customerId) {
        return BaseResult.of(produceService.toBePackageShelfList(customerId, ProduceItem.STATUS_PACKAGED));
    }

    @ApiOperation(value = "出标签", tags = TAG)
    @GetMapping("tip-detail")
    public BaseResult<TipDetail> tipDetail(@RequestParam Long shelfId) {
        return BaseResult.of(produceService.tipDetail(shelfId));
    }


}
